//引入express模块
const express=require('express');
//引入连接池对象
const pool=require('../pool.js');
//console.log(pool);
//const app=express();
//创建路由器对象
const r=express.Router();
// 轮播接口（get  /swiperson）
r.get('',(req,res,next)=>{
    // const p=req.query.pname
    // console.log(p);
    pool.query('select * from oil_swiper order by mid desc',[],(err,result)=>{
        if(err){
            next(err);
            return;
        }else{
        console.log(result);
    }
        res.send(result);
    })
})
// 增加
r.post('/swiperson',(req,res,next)=>{
    pool.query('insert into oil_swiper set ?',[req.body],(err,result)=>{
        if(err){
            throw err;
        }
        console.log(result);
    })
    res.send({code:200,msg:'油价资讯消息添加成功'})
})
// 删除
r.delete('/:mid',(req,res,next)=>{
    pool.query('delete from oil_swiper where mid=?',[req.params.mid],(err,result)=>{
        if(err){
            throw err;
        }
        console.log(result);
    })
    res.send({code:201,msg:'油价资讯消息删除成功'})
})
// 查询最后一条轮播
r.get('/lun',(req,res,next)=>{
    // const p=req.query.pname
    // console.log(p);
    pool.query('select * from oil_swiper order by mid desc limit 1' ,[],(err,result)=>{
        if(err){
            next(err);
            return;
        }else{
        console.log(result);
    }
        res.send(result);
    })
})


//暴露(导出)路由器对象
module.exports=r;